package boot.dao;

import boot.entity.Supplier;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface SupplierRepository extends JpaRepository<Supplier, Integer> , JpaSpecificationExecutor<Supplier> {
    List<Supplier> findByName(String name);

    //根据供应商id查找
    Supplier findById(int id);

    //根据供应商名字是否包含所给字符串来查找
    List<Supplier> findByNameContaining(String name);

    //查找库存大于给定数量的供应商
    List<Supplier> findByStockAfter(int stock);

    //查找名字与所给字符串正则匹配, 库存大于200的供应商, 若查询条件为空, 则不作为查询条件
    @Query(value = "select s from Supplier s where (?1 is null or s.name like %?1%) and  (?2 is null or s.stock > ?2)")
    List<Supplier> findWithJPQLByNameAndStock(String name, int stock);

}
